package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.dto.course.CourseDto;
import com.qk.entity.Course;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.CourseService;
import com.qk.vo.CourseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result getCourseList(CourseDto courseDto) {
        log.info("接收到请求，参数：{}", courseDto);
        PageResult<CourseVO> coursepageResult = courseService.getCourseList(courseDto);
        return Result.success(coursepageResult);
    }
    @DeleteMapping("/{id}")
    @LogOperation
    public Result deleteById(@PathVariable("id") Integer courseid) {
        log.info("删除课程：{}", courseid);
        courseService.deleteById(courseid);
        return Result.success();
    }
    @PostMapping
    @LogOperation
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程：{}", course);
        courseService.addCourse(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer courseid) {
        log.info("根据ID查询课程：{}", courseid);
        Course course = courseService.selectById(courseid);
        return Result.success(course);
    }

    @PutMapping
    @LogOperation
    public Result updateCourse(@RequestBody Course course) {
        log.info("更新课程：{}", course);
        courseService.updateCourse(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result selectList() {
        log.info("查询所有课程");
        List<Course> courseList = courseService.selectList();
        return Result.success(courseList);
    }
    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable("subject") Integer subject) {
        log.info("查询学科下所有所有课程：{}", subject);
        List<Course> courseList = courseService.selectBySubject(subject);
        return Result.success(courseList);
    }
}
